import smtplib
from email.mime.text import MIMEText
import sys

def sendMessage(text,receiver="429504198@qq.com"):
    sender = '429504198@qq.com'
    # receiver = '429504198@qq.com'
    smtp_server = 'smtp.qq.com'
    smtp_port = 587
    username = '429504198@qq.com'
    password = 'cyqvdaperovhbgfj'

    message = MIMEText("这里是邮件的正文内容", 'html')
    message['From'] = sender  # 直接设置字符串作为 "From" 头部
    message['To'] = receiver  # 直接设置字符串作为 "To" 头部
    message['Subject'] = text  # "Subject" 可以直接设置为字符串

    try:
        smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
        smtp_obj.ehlo()
        smtp_obj.starttls()  # 启用 TLS 加密
        smtp_obj.login(username, password)
        smtp_obj.sendmail(sender, receiver, message.as_string())
        print('邮件发送成功')
    except Exception as e:
        print('邮件发送失败')
        print(e)
    finally:
        if 'smtp_obj' in locals() and smtp_obj:
            smtp_obj.quit()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        subject = sys.argv[1]
        sendMessage(subject,sys.argv[2])
    else:
        print("请提供邮件主题")
